package com.test.io;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ByteStream
{

	public static void main(String[] args)
	{
//		tt();
		
//		bytetoFile();
		
		FiletoByte();
	}

	
	private static void FiletoByte()
	{
		File  f = new File("d:\\a.bt");
		FileInputStream in = null;
		byte[] buf = new byte[1024];
		try
		{
			in = new FileInputStream(f) ;
			int count = in.read(buf);
			
			for(int i = 0 ;i< count ;i++){
				System.out.printf("%04x\n", buf[i]);
			}
			
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}


	/**
	 * byte写到文件
	 * 
	 * 效率？flash？
	 * 2012
	 */
	private static void bytetoFile()
	{
		// TODO Auto-generated method stub
		
		File  f = new File("d:\\a.bt");
		FileOutputStream out = null ;
		byte[] b = {0x21,0x21,0x21,0x21,0x21,0x21,0x10};
		
		try
		{
			out = new FileOutputStream(f);
			
			out.write(b);
			
			out.flush();
			
		}
		catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(out != null){
				try
				{
					out.close();
				}
				catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}

	public static void tt()
	{

		int a = 0;
		int b = 1;
		int c = 2;
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		bout.write(b);
		bout.write(c);
		
		byte[] buff = bout.toByteArray();
		for (int i = 0; i < buff.length; i++)
			System.out.println(buff[i]);
		
		
		
		System.out.println("********ByteArrayInputStream***************");
		ByteArrayInputStream bin = new ByteArrayInputStream(buff);
		while ((b = bin.read()) != -1)
		{
			System.out.println(b);
		}
	}

}
